/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dms.dto.DormitoryDTO;
import dms.model.Building;
import dms.model.Dormitory;
import dms.model.util.PageIterator;
import dms.service.BuildingService;
import dms.service.DormitoryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;

/**
 * @author leejohn
 *
 */
public class DormitoryAction extends DispatchAction {
	
	/**
	 * 通过buildingId 获得该楼栋的所有寝室
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		int[] params = null;
		params = PageUtil.parsePageParameter(request);
		int start = params[0];
		int count = params[1];
		Long buildingId = null;
		try {
			buildingId = Long.valueOf(request.getParameter("buildingId"));
			Building building = buildingService.findById(buildingId);
			PageIterator<Dormitory> dormitories = dormitoryService.findByBuilding(start, count, building);
			request.setAttribute("dormitories", dormitories);
			request.setAttribute("building", building);
		} catch (Exception e) {			
			log.error(e.getMessage());
			return mapping.findForward(Constant.ERROR);
		}
		
		
		
		return mapping.findForward("list");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		DormitoryDTO dto = (DormitoryDTO) form;
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");

		try {
			dormitoryService.createDormitory(dto);
		} catch (Exception e) {
			return mapping.findForward(Constant.FAILURE);
		}
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long buildingId = null;
		try {
			buildingId = Long.valueOf(request.getParameter("buildingId"));
		} catch (Exception e) {
			mapping.findForward(Constant.FAILURE);
		}
		
		request.setAttribute("buildingId", buildingId);
		return mapping.findForward("addForm");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		DormitoryDTO dto = (DormitoryDTO) form;
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");

		try {
			dormitoryService.updateDormitory(dto);
		} catch (Exception e) {
			return mapping.findForward(Constant.FAILURE);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long dormitoryId = null;
		try {
			dormitoryId = Long.valueOf(request.getParameter("id"));
		} catch (Exception e) {
		
			return mapping.findForward(Constant.FAILURE);
		}
		
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		Dormitory dormitory = dormitoryService.findById(dormitoryId);
		
		request.setAttribute("dormitory", dormitory);
		return mapping.findForward("updateForm");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long dormitoryId = null;
		try {
			dormitoryId = Long.valueOf(request.getParameter("id"));
			DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
			dormitoryService.deleteById(dormitoryId);
		} catch (Exception e) {
		
			return mapping.findForward(Constant.FAILURE);
		}
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		
		return mapping.findForward(Constant.SUCCESS);
		
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("unspecified");
	}
}
